package demo.api;

import lombok.Builder;
import lombok.Data;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.http.Body;
import retrofit2.http.POST;
import retrofit2.http.Query;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by rock on 17/3/29.
 */
public interface CtmsService {

    /**
     * 创建订单
     * @param orderEntities
     * @return
     * @throws IOException
     */
    Dto.CtmsResponse createOrder(List<Dto.OrderEntity> orderEntities) throws IOException;

    /**
     * 撤销订单
     * @param numbers
     * @return
     * @throws IOException
     */
    Dto.CtmsResponse cancelOrders(List<String> numbers) throws IOException;

    /**
     * 更新批次
     * @param orderEntities
     * @return
     * @throws IOException
     */
    Dto.CtmsResponse updateBatch(List<Dto.OrderEntity> orderEntities) throws IOException;

    /**
    *授权
    *@param token
    **/
    void authorize(String token);


    interface RestApi {

        @POST("/order/create_order")
        Call<Dto.CtmsResponse> createOrder(@Query("access_token") String token, @Body Dto.OrderRequest request) throws IOException;

        @POST("/order/cancel_orders")
        Call<Dto.CtmsResponse> cancelOrders(@Query("access_token") String token, @Body Dto.CancelOrderRequest request) throws IOException;

        @POST("/order/update_batch")
        Call<Dto.CtmsResponse> updateBatch(@Query("access_token") String token,  @Body Dto.OrderRequest request) throws IOException;
    }

    interface Dto {

        @Data
        class OrderEntity {

            private String batchNumber;//批次号(必填)
            private String orderNumber;//订单号
            private Integer deliveryTime; // 提货时间,时间戳(必填)
            private String customerOrderNumber; // 客户单号
            private String startAddress; // 起始地
            private String endAddress; // 目的地
            private String consignerName;//发货人姓名(必填)
            private String consignerPhone;//发货人电话
            private String consignerAddress;//发货人地址(必填)
            private String consignerProvince; // 发货人所在省份
            private String consignerCity;//发货人所在城市(必填)
            private String consignerDistrict;//发货人所在地区

            private String consigneeName;//收货人姓名(必填)
            private String consigneePhone;//收货人电话(必填)
            private String consigneeAddress;//收货人地址(必填)
            private String consigneeProvince; // 收货人所在省份
            private String consigneeCity;//收货人所在城市(必填)
            private String consigneeDistrict;//收货人所在地区

            private String cargoName;//货物名称(必填)
            private String cargoQuantity; // 货物件数
            private String cargoWeight; // 货物重量
            private String cargoVolume; // 货物体积
            private String warehouseCode;
            private String ownerCode;
            private String scheduleOrderCode;
            private String orderType;

            private String deliveryArea; // 配送区域
            private Integer preDeliveryTime; // 预约送货时间
            private String payMode; // 付款方式

            private Map<String, Object> extraFields; // 扩展字端

            private String organizationPath; // 所属组织路径
        }

        @Data
        @Builder
        class CtmsResponse implements Serializable {

            private int code;

            private String message;

            private Object data;

            public static CtmsResponse create(Response response) {
                return CtmsResponse.builder()
                        .code(response.code())
                        .message(response.message())
                        .build();
            }
        }

        @Data
        @Builder
        class OrderRequest  {
            private List<OrderEntity> orderList = new ArrayList<>();
        }

        @Data
        @Builder
        class CancelOrderRequest  {
            private List<String> numbers = new ArrayList<>();
        }


    }


}
